package com.rocksdata.mc.business;

import com.rocksdata.mc.BuildConfig;
import com.rocksdata.mc.entity.PreviewInfo;
import com.rocksdata.mc.entity.WeekTimetableInfo;
import com.rocksdata.mc.http.HttpClient;
import com.rocksdata.mc.entity.CourseInfo;
import com.rocksdata.mc.entity.HomeworkInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 陈堂正 on 2017/4/14.
 */

public class TimetableManager implements BusinessInterface.ITimetableManager{
    private static final String URL_GET_WEEK_TIMETABLE = BuildConfig.HOST + "/schedule/weekLessons";
    private static final String URL_PREVIEW = BuildConfig.HOST + "/schedule/previews";
    private static final String URL_HOMEWORK = BuildConfig.HOST + "/schedule/works";

    private static BusinessInterface.ITimetableManager instance;

    private TimetableManager(){

    }

    synchronized static BusinessInterface.ITimetableManager getInstance(){
        if(instance == null)
            instance = new TimetableManager();
        return instance;
    }

    @Override
    public void getWeekTimetable(final BusinessInterface.OnBusinessResultListener listener){
        Map<String, Object> map = new HashMap<>();
        map.put("userID", UserInfoManager.getCurrentUserId());
        map.put("type", UserInfoManager.getCurrentUserType());
        HttpClient.get(URL_GET_WEEK_TIMETABLE, map, WeekTimetableInfo.class, new HttpClient.OnHttpResultListener<WeekTimetableInfo>() {
            @Override
            public void onResult(boolean isSuccess, List<WeekTimetableInfo> data, String msg) {
                if(isSuccess)
                    listener.onBusinessResult(true, data.get(0), null);
                else
                    listener.onBusinessResult(false, null, msg);

            }
        });
    }

    @Override
    public void releasePreview(CourseInfo info, String content, final BusinessInterface.OnBusinessResultListener listener){
        Map<String, String> map = new HashMap<>();
        map.put("detailID", info.getId());
        map.put("content", content);
        HttpClient.post(URL_PREVIEW, map, null, new HttpClient.OnHttpResultListener<String>() {
            @Override
            public void onResult(boolean isSuccess, List<String> data, String msg) {
                listener.onBusinessResult(isSuccess, null, msg);
            }
        });
    }

    @Override
    public void getPreview(CourseInfo info, final BusinessInterface.OnBusinessResultListener listener){
        Map<String, Object> map = new HashMap<>();
        map.put("detailID", info.getId());

        HttpClient.get(URL_PREVIEW, map, PreviewInfo.class, new HttpClient.OnHttpResultListener<PreviewInfo>() {
            @Override
            public void onResult(boolean isSuccess, List<PreviewInfo> data, String msg) {
                listener.onBusinessResult(isSuccess, data != null ? data.get(0) : null, msg);
            }
        });
    }

    @Override
    public void releaseHomework(CourseInfo info, String content, final BusinessInterface.OnBusinessResultListener listener){
        Map<String, String> map = new HashMap<>();
        map.put("detailID", info.getId());
        map.put("content", content);
        HttpClient.post(URL_HOMEWORK, map, null, new HttpClient.OnHttpResultListener<String>() {
            @Override
            public void onResult(boolean isSuccess, List<String> data, String msg) {
                listener.onBusinessResult(isSuccess, null, msg);
            }
        });
    }

    @Override
    public void getHomework(CourseInfo info, final BusinessInterface.OnBusinessResultListener listener){
        Map<String, Object> map = new HashMap<>();
        map.put("id", info.getId());

        HttpClient.get(URL_HOMEWORK, map, HomeworkInfo.class, new HttpClient.OnHttpResultListener<HomeworkInfo>() {
            @Override
            public void onResult(boolean isSuccess, List<HomeworkInfo> data, String msg) {
                listener.onBusinessResult(isSuccess, data != null ? data.get(0) : null, msg);
            }
        });
    }
}
